﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdapterTool_Demo
{
    public partial class PrintPrevForm : Form
    {
        //public bool m_color;

        public PrintPrevForm()
        {
            //m_color = false;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();

            Margins margin = new Margins(20, 20, 20, 20);
            pd.DefaultPageSettings.Margins = margin;

            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

            //PrintPreviewDialog ppdi = new PrintPreviewDialog();
            //ppdi.Document = pd;
            //ppdi.ShowDialog();
            //return;

            PrintDialog printd1 = new PrintDialog();
            printd1.ShowNetwork = false;
            printd1.PrintToFile = false;
            printd1.Document = pd;
            if (printd1.ShowDialog() != DialogResult.Cancel)
            {
                try
                {
                    pd.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
                }
            }
        }

        // pd_PrintPage 
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            Image temp = pictureBox1.Image;
            //GetResultIntoImage(ref temp);
            //pictureBox1.Image = temp;
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = temp.Width;
            int height = temp.Height;
            Rectangle destRect = new Rectangle(x, y, width, height);
            e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width,
                temp.Height, System.Drawing.GraphicsUnit.Pixel);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // view all picture
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            int x = this.Width;
            int y = this.Height;
            pictureBox1.Width = x - 100;
            pictureBox1.Height = y - 100;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // zoom in
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Width = (int)(1.10 * (double)pictureBox1.Width);
            pictureBox1.Height = (int)(1.10 * (double)pictureBox1.Height);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // zoom out
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Width = (int)(0.90 * (double)pictureBox1.Width);
            pictureBox1.Height = (int)(0.90 * (double)pictureBox1.Height);
        }
    }
}
